package com.example.attendancesystem_1_8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.attendancesystem_1_8.backendInterface.LoginThread;

//import static android.os.Build.VERSION_CODES.R;


public class MainActivity extends AppCompatActivity {

    private static int result = -1;//用于子进程回调
    private TextView tv_goto_register;
    private Button bt_login;
    private EditText et_account;
    private EditText et_password;
    private String account_input;
    private String password_input;

    //直接进入主界面按钮 用于测试
    private Button button_gotoMain;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_goto_register=(TextView)findViewById(R.id.tv_go_to_register);
        bt_login=(Button)findViewById(R.id.bt_login);
        et_password=(EditText)findViewById(R.id.et_pw_login);
        et_account =(EditText)findViewById(R.id.et_account_login);

        //直接进入主界面
        button_gotoMain = findViewById(R.id.button_gotoMain);
        button_gotoMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                DakaFragment dakaFragment = new DakaFragment();
                FragmentManager fragmentManager;
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.id_tab_1, dakaFragment).commit();
                Intent i = new Intent(MainActivity.this, DakaFragment.class);
                startActivity(i);
                 */
                /*
                Intent i = new Intent();
                i.setClass(MainActivity.this,MainActivity2.class);
                i.putExtra("id",0);//其中1表示Fragment的编号，从0开始编，secondFra的编号为1
                startActivity(i);

                 */
                Intent i = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(i);
                finish();

            }
        });


        tv_goto_register.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        bt_login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if(account_input == null){
                    Toast.makeText(MainActivity.this,"用户名不能为空",Toast.LENGTH_LONG).show();
                }
                else if(password_input == null){
                    Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
                }
                else if(account_input.length() < 6 || account_input.length() > 18){
                    Toast.makeText(MainActivity.this,"账号应为6-18个字符",Toast.LENGTH_LONG).show();
                }
                else if(password_input.length() < 6 || password_input.length() > 18){
                    Toast.makeText(MainActivity.this,"密码应为6-18个字符",Toast.LENGTH_LONG).show();
                }
                else{

                    Thread thread=new Thread(new LoginThread(account_input, password_input),"threadName");
                    thread.start();
                    try {
                        thread.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    switch (result){
                        case 0:
                            Toast.makeText(MainActivity.this,"登录成功，即将跳转",Toast.LENGTH_LONG).show();
                            SystemClock.sleep(2000);
                            Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                            startActivity(intent);
                            break;
                        case 1:
                            Toast.makeText(MainActivity.this,"账号不存在，登录失败",Toast.LENGTH_LONG).show();
                            break;
                        case 2:
                            Toast.makeText(MainActivity.this,"密码错误，登录失败",Toast.LENGTH_LONG).show();
                            break;
                        case 3:
                            Toast.makeText(MainActivity.this,"未知原因导致登录失败",Toast.LENGTH_LONG).show();
                            break;
                    }
                    result = -1;

                }

                /*
                else{
                    int result;
                    result = login(username_input, password_input);
                    switch (result){
                        case 0:
                            Toast.makeText(MainActivity.this,"用户名不存在",Toast.LENGTH_LONG).show();
                            break;
                        case 1:
                            Toast.makeText(MainActivity.this,"登陆成功，即将跳转",Toast.LENGTH_LONG).show();
                            SystemClock.sleep(2000);
                            Intent intent = new Intent(MainActivity.this, MainActivity.class);
                            startActivity(intent);
                            break;
                        case 2:
                            Toast.makeText(MainActivity.this,"密码错误",Toast.LENGTH_LONG).show();
                            break;
                    }
                }

                 */
            }
        });


        et_account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                account_input = charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                password_input = charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }
    //回调函数
    public static void callback(int backtype){
        result = backtype;
    }

    /*
    private int login(String username, String password){
        for (String key : Constant.mapUser.keySet()) {
            if(key.equals(username)){
                if(Constant.mapUser.get(key).equals(password)){
                    return 1;
                }else{
                    return 2;
                }
            }
        }
        return 0;
    }

     */


   /*
   private void setListeners(){
        OnClick onClick = new OnClick();
        tv_goto_register.setOnClickListener(onClick);
    }
    private class OnClick implements View.OnClickListener{
        public void onClick(View v){
            Intent intent = null;
            switch (v.getId()){
                case R.id.tv_go_to_register:
                    intent = new Intent(MainActivity.this, RegisterActivity.class);
                    break;
            }
            startActivity(intent);
        }
    }

    */
}